package com.util.common;

import com.github.pagehelper.PageInfo;
import com.google.common.primitives.Ints;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/27 0027.
 */
public class Page implements Serializable {
    private int pageSize = 20;
    private int page = 1;
    private int count;
    private int pages;
    private static int MAX_PAGE_SIZE = 20;
    protected boolean autoCount = true;

    public Page(int pageSize, int page){
        this.pageSize = pageSize;
        this.page = page;
    }

    public Page(int pageSize, int page, int count) {
        if(pageSize > MAX_PAGE_SIZE) {
            this.pageSize = MAX_PAGE_SIZE;
        } else {
            this.pageSize = pageSize;
        }

        this.page = page;
        this.count = count;
        if(this.pageSize == 0) {
            this.pages = 0;
        } else if(this.count % this.pageSize == 0) {
            this.pages = this.count / this.pageSize;
        } else {
            this.pages = count / this.pageSize + 1;
        }

    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        if(pageSize > MAX_PAGE_SIZE) {
            this.pageSize = MAX_PAGE_SIZE;
        } else {
            this.pageSize = pageSize;
        }

    }

    public int getPage() {
        return this.page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getCount() {
        return this.count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getPages() {
        return this.pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public Page() {
    }

    public Page(int pageSize, int page, int count, int pages) {
        this.pageSize = pageSize;
        this.page = page;
        this.count = count;
        this.pages = pages;
    }

    public static Page toPage(List<?> obj) {
        PageInfo pageInfo = new PageInfo(obj);
        long total = pageInfo.getTotal();
        return new Page(pageInfo.getPageSize(), pageInfo.getPageNum(), Ints.saturatedCast(total), pageInfo.getPages());
    }

    public static Map<String, Object> toPageMap(List<?> obj, Page page) {
        HashMap map = new HashMap();
        map.put("items", obj);
        map.put("page", page);
        return map;
    }

    public Integer getOffset() {
        if(this.getPageSize() < 0) {
            throw new IllegalArgumentException("[pageSize] must great than zero");
        } else {
            return Integer.valueOf((this.getPage() - 1) * this.getPageSize());
        }
    }

    public int maxPageSize() {
        return MAX_PAGE_SIZE;
    }

    /**
     * 获得查询对象时是否先自动执行Count查询获取总记录数, 默认为false.
     */
    public boolean autoCount() {
        return autoCount;
    }

    /**
     * 设置查询对象时是否自动先执行Count查询获取总记录数.
     */
    public void disableAutoCount(final boolean autoCount) {
        this.autoCount = autoCount;
    }
}
